package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.SalaryDAO;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.SalaryProfile;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.dao.model.ViewSalaryProfileDetail;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.ui.control.CustomTextField;

public class SalaryProfileController extends AbstractUIController implements
		FormValidator {

	private Company company;
	private UserAccounts userAcounts;
	private ViewSalaryProfileDetail detail;
	@FXML
    private CustomTextField basic;
    @FXML
    private CustomTextField conveyance;
    @FXML
    private CustomTextField esi;
    @FXML
    private CustomTextField hra;
    @FXML
    private CustomTextField medicalAllowance;
    @FXML
    private CustomTextField pfFund;
    @FXML
    private TextArea profileDesc;
    @FXML
    private TextField profileName;
    @FXML
    private Parent root;
    @FXML
    private CustomTextField bonus;
    @FXML
    private CustomTextField pfMaxLimit;
    @FXML
    private CustomTextField specialAllowance;
    @FXML
    private CustomTextField others;
    @FXML
    Button save;
    SalaryDAO salaryDAO = PayrollApplicationFactory.getSalaryDAO();
    String operation = null;

    public void goBack() {
		
		if (getParent() != null && getParent().length() > 0) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}
    
	}

    public void reset() {
    	refreshUI(company,userAcounts,detail);
    }

    public void save() {
    	
    	String validationMsg = getValidationMessage();
    	
    	if(!validationMsg.equals("")){
    		showError(validationMsg);
    		return;
    	}
    	SalaryProfile salaryProfile = new SalaryProfile();
    	salaryProfile.setBasic(basic.getText());
    	salaryProfile.setBonus(bonus.getText());
    	salaryProfile.setConveyance(conveyance.getText());
    	salaryProfile.setDesc(profileDesc.getText());
    	salaryProfile.setEsi(esi.getText());
    	salaryProfile.setHra(hra.getText());
    	salaryProfile.setMaxPF(pfMaxLimit.getText());
    	salaryProfile.setMedicalAllo(medicalAllowance.getText());
    	salaryProfile.setPfPer(pfFund.getText());
    	salaryProfile.setProfileName(profileName.getText());
    	salaryProfile.setSpecialAllow(specialAllowance.getText());
    	salaryProfile.setOthers(others.getText());
		try {

			if (detail != null) {
				salaryDAO.modifySalaryProfile(salaryProfile);

			} else {
				salaryDAO.addSalaryProfile(salaryProfile);
				save.setDisable(true);
			}
		} catch (BusinessException e) {
			e.printStackTrace();
			showError(e.getMessage());
			refreshUI();
			return;
		}
    	
		showInfo("User profile information saved successfully");
    }

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {


    }

	@Override
	public boolean isValid() {
		return false;
	}

	@Override
	public String getValidationMessage() {
		
		if(basic.getText().trim().equals(""))
			return "Basic percent can not be empty";
		if(hra.getText().trim().equals(""))
			return "HRA percent can not be empty";
		if(profileName.getText().trim().equals(""))
			return "Profile name can not be empty";
		if(profileDesc.getText().trim().equals(""))
			return "Profile name can not be empty";
		
		if((!pfFund.getText().trim().equals("") && !pfFund.getText().trim().equals("0"))){
		
			if((!pfMaxLimit.getText().trim().equals("") && !pfMaxLimit.getText().trim().equals("0")))
				return "please enter either PF MAx amount or PF percentage";
		}
		
		if((!pfMaxLimit.getText().trim().equals("") && !pfMaxLimit.getText().trim().equals("0"))){
			if((!pfFund.getText().trim().equals("") && !pfFund.getText().trim().equals("0")))
			return "please enter either PF MAx amount or PF percentage";
		}
		return "";
	}

	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return "Salary Profiles";
	}

	@Override
	public void refreshUI(Object... args) {

		if (args != null && args.length > 0) {
			Company company = (Company) args[0];
			this.company = company;

			if (args.length > 1 && args[1] instanceof UserAccounts) {
				userAcounts = (UserAccounts) args[1];
			}
			
			if(args.length > 2 && args[2] instanceof ViewSalaryProfileDetail){
				detail = (ViewSalaryProfileDetail)args[2];
				
			}
		}
		if(detail == null){
			
			basic.clear();
			conveyance.clear();
			esi.clear();
			hra.clear();
			medicalAllowance.clear();
			profileDesc.clear();
			pfFund.clear();
			profileName.clear();
			bonus.clear();
			pfMaxLimit.clear();
			specialAllowance.clear();
			others.clear();
			save.setDisable(false);
		}else{
			
			
			basic.setText(detail.getBasic());
			conveyance.setText(detail.getConveyance());
			esi.setText(detail.getEsi());
			hra.setText(detail.getHra());
			medicalAllowance.setText(detail.getMedicalAllo());
			profileDesc.setText(detail.getDesc());
			pfFund.setText(detail.getPfPer());
			profileName.setText(detail.getProfileName());
			bonus.setText(detail.getBonus());
			pfMaxLimit.setText(detail.getMaxPF());
			specialAllowance.setText(detail.getSpecialAllo());
			others.setText(detail.getOthers());
		}
	}

}
